package cn.edu.sctu.sctu_lysd.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.sctu.sctu_lysd.R;
import cn.edu.sctu.sctu_lysd.bean.ParcelData;
import cn.edu.sctu.sctu_lysd.my_view.RoundImageView;
import cn.edu.sctu.sctu_lysd.utils.JumpUtils;
import cn.edu.sctu.sctu_lysd.view.act.OrderDetailedAct;

/**
 * Created by wangfuxin on 2017-04-13.
 */

public class MyOrdersLeftListAda extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<ParcelData> dataList;

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;

    private View footerView;

    public void dataChange(boolean isRe, List<ParcelData> dataList) {

        if (isRe) {
            this.dataList = dataList;
        } else {

            this.dataList.addAll(dataList);
        }

        this.notifyDataSetChanged();

    }

    public MyOrdersLeftListAda(Context mContext, List<ParcelData> dataList) {
        this.mContext = mContext;
        this.dataList = dataList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.item_my_orders, null);
            view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,
                    RecyclerView.LayoutParams.WRAP_CONTENT));


            return new ItemViewHolder(view);
        }
        // type == TYPE_FOOTER 返回footerView
        else if (viewType == TYPE_FOOTER) {
            footerView = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.footerview, null);
            footerView.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,
                    RecyclerView.LayoutParams.WRAP_CONTENT));
            return new FooterViewHolder(footerView);
        }

        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        if (holder instanceof ItemViewHolder) {


            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Bundle bundle = new Bundle();

                    bundle.putLong(OrderDetailedAct.INTENT_ID_TAG, dataList.get(position).getId());
                    bundle.putInt(OrderDetailedAct.INTENT_IS_MY_ORDER_TAG, OrderDetailedAct.MY_ORDER);

                    JumpUtils.jumpActivity(mContext, OrderDetailedAct.class, bundle);
                }
            });

            ((ItemViewHolder) holder).goodsNameTxt.setText(dataList.get(position).getName());
            ((ItemViewHolder) holder).goodsWeightTxt.setText(dataList.get(position).getWeight() + "kg");
            ((ItemViewHolder) holder).goodsDescribe.setText(dataList.get(position).getDescription());
            Picasso.with(mContext)
                    .load(dataList.get(position).getParcelPhotoUrl())
                    .into(((ItemViewHolder) holder).goodsImg);

        }

    }

    @Override
    public int getItemCount() {

        if (dataList != null) {
            return dataList.size() + 1;
        }

        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    class FooterViewHolder extends RecyclerView.ViewHolder {

        public FooterViewHolder(View view) {
            super(view);
        }

    }


    class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView goodsNameTxt, goodsWeightTxt, goodsDescribe;
        ImageView goodsImg;


        public ItemViewHolder(View itemView) {
            super(itemView);
            goodsNameTxt = (TextView) itemView.findViewById(R.id.item_goods_name_txt);
            goodsWeightTxt = (TextView) itemView.findViewById(R.id.item_goods_weight_txt);
            goodsDescribe = (TextView) itemView.findViewById(R.id.item_goods_description_txt);
            goodsImg = (ImageView) itemView.findViewById(R.id.item_goods_img);
        }
    }


}
